package uk.ac.bath.domains.misc;


import uk.ac.bath.legacy.Point;
import uk.ac.bath.util.*;
/*
 * Cyclic2DSpace.java
 *
 * Created on January 23, 2001, 1:38 PM
 */

public class Cyclic2DSpace extends Space2D {

    /** Creates new Cyclic2DSpace */
    public Cyclic2DSpace(int sizeX, int sizeY) {
        super(sizeX, sizeY, 1);
    }

    public  Point randomPointNear(Point p1, int rad) {
        Point2DInt p = rawRandomPointNear(p1, rad);
        if (p.x >= sizeX) {
            p.x = p.x - sizeX;
        } else if (p.x < 0) {
            p.x = p.x + sizeX;
        }
        if (p.y >= sizeY) {
            p.y = p.y - sizeY;
        } else if (p.y < 0) {
            p.y = p.y + sizeY;
        }

        return p;
    }

    public boolean contains(Point p1) {
        Point2DInt p = (Point2DInt) p1;
        if (p.x >= sizeX) {
            p.x = p.x - sizeX;
        } else if (p.x < 0) {
            p.x = p.x + sizeX;
        }
        if (p.y >= sizeY) {
            p.y = p.y - sizeY;
        } else if (p.y < 0) {
            p.y = p.y + sizeY;
        }
        return true;
    }

    public  Point above(Point p1) {
        Point2DInt p = (Point2DInt) p1;
        int y = p.getIY() - 1;
        if (y < 0) {
            y = sizeY - 1;
        }
        int x = p.getIX();
        return new Point2DInt(x, y);
    }

    public  Point below(Point p1) {
        Point2DInt p = (Point2DInt) p1;
        int y = p.getIY() + 1;
        if (y >= sizeY) {
            y = 0;
        }
        int x = p.getIX();

        return new Point2DInt(x, y);
    }

    public Point left(Point p1) {
        Point2DInt p = (Point2DInt) p1;
        int x = p.getIX() - 1;
        if (x < 0) {
            x = sizeX - 1;
        }
        int y = p.getIY();
        return new Point2DInt(x, y);
    }

    public Point right(Point p1) {
        Point2DInt p = (Point2DInt) p1;
        int x = p.getIX() + 1;
        if (x >= sizeX) {
            x = 0;
        }
        int y = p.getIY();
        return new Point2DInt(x, y);
    }

    public Point randomPoint() {
        Point p = new Point2DInt(MyRandom.nextInt(sizeX), MyRandom.nextInt(sizeY));
        return p;
    }
}

